package com.qingcheng.service.impl;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.ReturnOrderMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.TuiHu;
import com.qingcheng.pojo.order.ReturnOrder;
import com.qingcheng.service.order.ReturnOrderService;
import com.qingcheng.service.system.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ReturnOrderServiceImpl implements ReturnOrderService {

    @Autowired
    private ReturnOrderMapper returnOrderMapper;

    @Reference
    private AdminService adminService;

    /**
     * 返回全部记录
     * @return
     */
    public List<ReturnOrder> findAll() {
        return returnOrderMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<ReturnOrder> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<ReturnOrder> returnOrders = (Page<ReturnOrder>) returnOrderMapper.selectAll();
        return new PageResult<ReturnOrder>(returnOrders.getTotal(),returnOrders.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<ReturnOrder> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return returnOrderMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public TuiHu findPage(Map<String, Object> searchMap, Integer page, Integer size) {


        PageResult<ReturnOrder> pageResult = new PageResult<>();
        TuiHu  tuiHu = new TuiHu();
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<ReturnOrder> returnOrders = (Page<ReturnOrder>) returnOrderMapper.selectByExample(example);
        pageResult.setRows(returnOrders.getResult());//list
        pageResult.setTotal(returnOrders.getTotal());
        tuiHu.setPageResult(pageResult);

        tuiHu.setSize(size);//显示个数
        tuiHu.setCurrentpage(page);//当前页
        tuiHu.setTotalpage(returnOrders.getPages());//总页数
        //开始页码，和结束页码
        int pages = returnOrders.getPages();//总页码
        int begin =1;
        int end =pages;

        if(pages<=5){

        }else {//总页数大于5
            begin=page-2;
            end=begin+4;
            if(begin<=1){
                begin=1;
            }
        }
        tuiHu.setBegin(begin);//开始页码
        tuiHu.setEnd(end);//结束页码
//        Interge  dclcount  //已申请      0
//        Interge  Thzcount  //退货中个数  1
//        Integre   Ywccont  //完成个数    3
//        Integre   Jjcount  //拒绝个数    2

        tuiHu.setDclcount(getCount("0"));
        tuiHu.setThzcount(getCount("1"));
        tuiHu.setYwccont(getCount("3"));
        tuiHu.setJjcount(getCount("2"));

        return tuiHu;
    }


    private Integer getCount(String status){
        ReturnOrder returnOrder = new ReturnOrder();
        returnOrder.setStatus(status);
        return returnOrderMapper.selectCount(returnOrder);
    }



    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public ReturnOrder findById(Long id) {
        return returnOrderMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param returnOrder
     */
    public void add(ReturnOrder returnOrder) {
        returnOrderMapper.insert(returnOrder);
    }

    /**
     * 修改
     * @param returnOrder
     */
    public void update(ReturnOrder returnOrder) {
        returnOrderMapper.updateByPrimaryKeySelective(returnOrder);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Long id) {
        returnOrderMapper.deleteByPrimaryKey(id);
    }


    @Override
    public boolean tuihuo(Long id, String admin) {
        ReturnOrder returnOrder = returnOrderMapper.selectByPrimaryKey(id);
        if(returnOrder==null){
            throw new RuntimeException("退货商品不存在");
        }


        //修改
        returnOrder.setStatus("1");

        returnOrder.setAdminId(adminService.findByname(admin));

        returnOrder.setDisposeTime(new Date());
        int boolen = returnOrderMapper.updateByPrimaryKeySelective(returnOrder);
            if(boolen==1){
                return true;
            }else {
                return false;
            }

    }

    @Override
    public boolean sorry(Long id, String name) {

        ReturnOrder returnOrder = returnOrderMapper.selectByPrimaryKey(id);
        if(returnOrder==null){
            throw new RuntimeException("退货商品不存在");
        }


        //修改
        returnOrder.setStatus("2");

        returnOrder.setAdminId(adminService.findByname(name));

        returnOrder.setDisposeTime(new Date());
        int boolen = returnOrderMapper.updateByPrimaryKeySelective(returnOrder);
        if(boolen==1){
            return true;
        }else {
            return false;
        }

    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(ReturnOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 用户账号
            if(searchMap.get("userAccount")!=null && !"".equals(searchMap.get("userAccount"))){
                criteria.andLike("userAccount","%"+searchMap.get("userAccount")+"%");
            }
            // 联系人
            if(searchMap.get("linkman")!=null && !"".equals(searchMap.get("linkman"))){
                criteria.andLike("linkman","%"+searchMap.get("linkman")+"%");
            }
            // 联系人手机
            if(searchMap.get("linkmanMobile")!=null && !"".equals(searchMap.get("linkmanMobile"))){
                criteria.andLike("linkmanMobile","%"+searchMap.get("linkmanMobile")+"%");
            }
            // 类型
            if(searchMap.get("type")!=null && !"".equals(searchMap.get("type"))){
                criteria.andLike("type","%"+searchMap.get("type")+"%");
            }
            // 是否退运费
            if(searchMap.get("isReturnFreight")!=null && !"".equals(searchMap.get("isReturnFreight"))){
                criteria.andLike("isReturnFreight","%"+searchMap.get("isReturnFreight")+"%");
            }
            // 申请状态
            if(searchMap.get("status")!=null && !"".equals(searchMap.get("status"))){
                criteria.andLike("status","%"+searchMap.get("status")+"%");
            }
            // 凭证图片
            if(searchMap.get("evidence")!=null && !"".equals(searchMap.get("evidence"))){
                criteria.andLike("evidence","%"+searchMap.get("evidence")+"%");
            }
            // 问题描述
            if(searchMap.get("description")!=null && !"".equals(searchMap.get("description"))){
                criteria.andLike("description","%"+searchMap.get("description")+"%");
            }
            // 处理备注
            if(searchMap.get("remark")!=null && !"".equals(searchMap.get("remark"))){
                criteria.andLike("remark","%"+searchMap.get("remark")+"%");
            }

            // 退款金额
            if(searchMap.get("returnMoney")!=null ){
                criteria.andEqualTo("returnMoney",searchMap.get("returnMoney"));
            }
            // 退货退款原因
            if(searchMap.get("returnCause")!=null ){
                criteria.andEqualTo("returnCause",searchMap.get("returnCause"));
            }
            // 管理员id
            if(searchMap.get("adminId")!=null ){
                criteria.andEqualTo("adminId",searchMap.get("adminId"));
            }

        }
        return example;
    }

}
